package com.tanhua.dubbo.api;

import com.tanhua.dubbo.api.mongo.FriendApi;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

/**
 * 好友服务接口实现类
 */
@DubboService
public class FriendApiImpl implements FriendApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存好友关系到好友表
     * @param userId
     * @param friendUserId
     */
    @Override
    public void makeFriends(Long userId, Long friendUserId) {
        long nowTime = System.currentTimeMillis();
        addFriend(userId,friendUserId,nowTime);
        addFriend(friendUserId,userId,nowTime);
    }

    /**
     * 联系人列表分页查询
     */
    @Override
    public PageResult<Friend> findPageContacts(Long page, Long pageSize, Long userId) {
        //定义返回PageResult<Friend>
        PageResult<Friend> pageResult = new PageResult<>();
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        //1查询总记录数
        long counts = mongoTemplate.count(query, Friend.class);
        long start = (page-1)*pageSize;
        //2查询当前页面需要展示的数据
        List<Friend> friendList = new ArrayList<>();
        if(counts > start){ //20 > 27
            //构造分页limit 0,3
            query.limit(pageSize.intValue());//每页查询几条
            query.skip(start);//跳过1条数据
            //query.with(PageRequest.of(page,pageSize));
            query.with(Sort.by(Sort.Direction.DESC,"userId"));
            friendList = mongoTemplate.find(query, Friend.class);
        }
        return new PageResult<>(page,pageSize,counts,friendList);
    }

    //调用服务查询好友表数量
    @Override
    public Long count(Long userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        return mongoTemplate.count(query,Friend.class);
    }

    //往好友表tanhua_users表中保存2条记录
    @Override
    public void saveFriend(Friend friend) {
        Long userId = friend.getUserId(); //当前用户id
        Long friendId = friend.getFriendId();//好友id
        //先根据userId friendId查询好友表记录是否存在
        Query query1 = new Query();
        query1.addCriteria(Criteria.where("userId").is(userId).and("friendId").is(friendId));
        //不存在 再保存
        if (!mongoTemplate.exists(query1, Friend.class)) {
            Friend f1 = new Friend();
            f1.setUserId(friend.getUserId());
            f1.setFriendId(friend.getFriendId());
            f1.setCreated(System.currentTimeMillis());//添加好友时间
            mongoTemplate.save(f1);
        }
        //3.先根据friendId userId 查询好友表记录是否存在
        Query query2 = new Query();
        query2.addCriteria(Criteria.where("userId").is(friendId).and("friendId").is(userId));
        //不存在 再保存
        if (!mongoTemplate.exists(query2, Friend.class)) {
            Friend f2 = new Friend();
            f2.setUserId(friend.getFriendId());
            f2.setFriendId(friend.getUserId());
            f2.setCreated(System.currentTimeMillis());//添加好友时间
            mongoTemplate.save(f2);
        }
    }

    /**
     * 添加好友公共方法
     * @param userId
     * @param friendUserId
     * @param nowTime
     */
    private void addFriend(Long userId, Long friendUserId, long nowTime) {
        //1.根据当前登录用户id 好友id 查询记录是否存在
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId).and("friendId").is(friendUserId));
        if(!mongoTemplate.exists(query, Friend.class)) {
            //2.不存在则保存记录
            Friend friend = new Friend();
            friend.setUserId(userId);
            friend.setFriendId(friendUserId);
            friend.setCreated(nowTime);
            mongoTemplate.insert(friend);
        }
    }
}
